package cn.stylefeng.guns.modular.productfunction.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.productfunction.entity.ShiHeadFunction;
import cn.stylefeng.guns.modular.productfunction.mapper.ShiHeadFunctionMapper;
import cn.stylefeng.guns.modular.productfunction.model.params.ShiHeadFunctionParam;
import cn.stylefeng.guns.modular.productfunction.model.result.ShiHeadFunctionResult;
import cn.stylefeng.guns.modular.productfunction.service.ShiHeadFunctionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *   头部功能表
 * @author 石博士
 * @since 2019-07-05
 */
@Service
public class ShiHeadFunctionServiceImpl extends ServiceImpl<ShiHeadFunctionMapper, ShiHeadFunction> implements ShiHeadFunctionService {

    @Override
    public ShiHeadFunction add(ShiHeadFunctionParam param){
        ShiHeadFunction entity = getEntity(param);
        this.save(entity);
        return entity;
    }

    @Override
    public void delete(ShiHeadFunctionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ShiHeadFunctionParam param){
        ShiHeadFunction oldEntity = getOldEntity(param);
        ShiHeadFunction newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ShiHeadFunctionResult findBySpec(ShiHeadFunctionParam param){
        return null;
    }

    @Override
    public List<ShiHeadFunctionResult> findListBySpec(ShiHeadFunctionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShiHeadFunctionParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ShiHeadFunctionParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ShiHeadFunction getOldEntity(ShiHeadFunctionParam param) {
        return this.getById(getKey(param));
    }

    private ShiHeadFunction getEntity(ShiHeadFunctionParam param) {
        ShiHeadFunction entity = new ShiHeadFunction();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
